from fastapi import APIRouter
from orm.models import *
from pydantic import BaseModel, Field
from fastapi import status
from common.error_codes import ErrorCode
from typing import Optional

api_department_create = APIRouter()


class DepartInfo(BaseModel):
    depart_name: str = Field(..., title="部门名称", description='部门名称')
    depart_hobby: Optional[str] = Field(..., title="部门爱好", description='部门爱好')


@api_department_create.post('/v1/create',
                            status_code=status.HTTP_200_OK,
                            description="创建部门")
async def create_xbase_depart_info(depart: DepartInfo):
    #需要判断创建的部门是否已经存在
    if await Department.filter(depart_name=depart.depart_name).first():
        return {
            'code':
            ErrorCode.DepartmentAlreadyExists,
            "message":
            ErrorCode.get_error_message(ErrorCode.DepartmentAlreadyExists)
        }
    test_info = await Department.create(depart_name=depart.depart_name,
                                        depart_hobby=depart.depart_hobby)
    return {
        "code": status.HTTP_200_OK,
        "msg": ErrorCode.get_error_message(ErrorCode.success),
        "name": test_info.depart_name
    }
